#include "Gamemap.h"
#include <QFile>
#include <QDebug>

GameMap::GameMap(QObject *parent) : QObject(parent)
{
    row=0;
    col=0;
    Gmap=nullptr;
}

GameMap::~GameMap(){
    if(Gmap != nullptr)
    {
        for(int i=0;i<row;i++)
        {
            delete [] Gmap[i];
        }
        delete [] Gmap;
    }
}

void GameMap::iniByTxt(QString fileName)
{
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"文件打开失败";
        return;
    }

    QByteArray arr=file.readAll();
    arr.replace("\r\n","\n");
    QList<QByteArray> linelist=arr.split('\n');
    row = linelist.size();

    Gmap = new int* [row];
    for(int i=0;i<row;i++)
    {
        QList<QByteArray> collist=linelist[i].split(',');
        col = collist.size();
        Gmap[i]=new int [col];
        for(int j=0;j<col;j++)
        {
            Gmap[i][j] = collist[j].toInt();
        }
    }

}
